package double_pointer;

public class MoveZeroes {
    /**
     * 自己写的
     * @param nums
     */
    public void MymoveZeroes(int[] nums) {
        if (nums.length == 1) {
            return;
        }
        int prev = 0;
        int cur = prev + 1;
        while (cur <= nums.length - 1) {
            if (nums[prev] == 0) {
                if (nums[cur] != 0) {
                    swap(nums, prev, cur);
                    prev++;
                    cur++;
                } else {
                    cur++;
                }
            } else {
                prev++;
                cur++;
            }
        }
    }

    /**
     * 参考答案
     * @param nums
     */
    public void moveZeroes(int[] nums) {
        for (int cur = 0, dest = -1; cur < nums.length; cur++) {
            if (nums[cur] != 0) {
                dest++;
                swap(nums,dest,cur);
            }
        }
    }

    private void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}
